/*
 * 文件名：ProPlotController.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年1月9日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.mkt.web;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.hr.service.StaffInfoService;
import top.infopub.mgr.mkt.domain.ProPlotDomain;
import top.infopub.mgr.mkt.service.PreProjectService;
import top.infopub.mgr.mkt.service.ProPlotService;
import top.infopub.mgr.mkt.vo.ProPlotVo;
import top.infopub.mgr.util.LogUtil;
import top.infopub.mgr.util.LogUtil.AcctionType;
import top.infopub.mgr.util.LogUtil.LogModelType;
import top.infopub.model.mkt.PreProject;
import top.infopub.model.mkt.ProPlot;
import top.infopub.security.util.SessionUserUtils;

import com.github.pagehelper.Page;


/**
 * 立项策划管理
 * @author wuzhiyang
 * @version 2020年1月9日
 * @see ProPlotController
 * @since
 */
@Controller
@RequestMapping("/proPlot")
public class ProPlotController extends GenericFlowController {

    @Autowired
    private ProPlotService proPlotService;

    @Autowired
    private PreProjectService preProjectService;

    @Autowired
    private StaffInfoService staffInfoService;

    /**
     * 立项策划列表
     * @return
     */
    @RequestMapping("/list")
    public String list() {
        return "mkt/lay-proPlot-list";
    }

    /**
     * 立项策划列表数据
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<ProPlotDomain> queryDataByCondition(ProPlotVo proPlotVo, ParamPage paramPage) {
        Page<ProPlotDomain> page = new Page<ProPlotDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (proPlotVo.getEndDate() != null) {
            proPlotVo.setEndDate(DateUtils.addDays(proPlotVo.getEndDate(), 1));
        }
        page = proPlotService.selectByDomainWhere(page, proPlotVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 立项策划-我审批的列表数据
     * @return
     */
    @RequestMapping("/queryMyCheckDataByCondition")
    @ResponseBody
    public RspVo<ProPlotDomain> queryMyCheckDataByCondition(ProPlotVo proPlotVo,
                                                            ParamPage paramPage) {
        Page<ProPlotDomain> page = new Page<ProPlotDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (proPlotVo.getEndDate() != null) {
            proPlotVo.setEndDate(DateUtils.addDays(proPlotVo.getEndDate(), 1));
        }
        // 增加addby
        proPlotVo.setAddBy(SessionUserUtils.getSessionUserId());
        page = proPlotService.selectMyCheckByDomainWhere(page, proPlotVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 立项策划-新增
     * @return
     */
    @RequestMapping("/add")
    public String toAdd(HttpServletRequest request) {
        //返回所有预立项中的项目名称
        List<PreProject> selectPreProject = preProjectService.selectPreProject(null);
        request.setAttribute("allProName", selectPreProject);
        return "mkt/lay-proPlot-add";
    }

    /**
     * 立项策划-新增
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(HttpServletRequest request, ProPlot proPlot) {
        String proCode = request.getParameter("proName");
        proPlot.setProCode(proCode);
        List<PreProject> preProject = preProjectService.selectPreProject(proCode);
        if (preProject.size() == 1) {
            for (PreProject preProject2 : preProject) {
//                proPlot.setProName(preProject2.getProName());
            }
        }
        proPlotService.saveProPLot(proPlot);
        LogUtil.bizLog(LogModelType.PROJECT, AcctionType.ADD,
            "市场事项 -新增立项策划：项目名=" + proPlot.getProName(), null);
        return RspVo.getSuccessResponse();
    }

    @RequestMapping("/edit")
    @ResponseBody
    public RspVo edit(HttpServletRequest request, ProPlot proPlot) {
        String submitType = request.getParameter("submitType");//2=提交  1保存
        proPlotService.editProPLot(proPlot,submitType);
        LogUtil.bizLog(LogModelType.PROJECT, AcctionType.ADD,
                "市场事项 -编辑立项策划：项目名=" + proPlot.getProName(), null);
        return RspVo.getSuccessResponse();
    }


    /**
     * 根据id查询预立项详情  > 注意：方法命名必须为：toDealOrDetail
     * @param id 预立项Id
     * @return
     * @see
     */
    @RequestMapping("/toDealOrDetail")
    public ModelAndView queryById(HttpServletRequest request, String id, String processDealWith) {
        Map<String, Object> map = new HashMap<String, Object>();
        ProPlotDomain proPlot = proPlotService.queryById(id);
        request.setAttribute("modelDetail", proPlot);
        map.put("proPlot", proPlot);
        // 审批flag
        request.setAttribute("processDealWith", processDealWith);
        // 审批data
        processData(request, proPlot.getProcessId());
        return new ModelAndView("mkt/lay-proPlot-detail", map);
    }

    @RequestMapping("/toDetail")
    public String queryDetailById(HttpServletRequest request, String id, String processDealWith) {
        ProPlotDomain proPlot= proPlotService.queryById(id);
        request.setAttribute("proPlot", proPlot);
        request.setAttribute("edit", "edit");
        return "mkt/lay-proPlot-add";
    }

    /**
     * 通过项目id查询记录
     */
    @RequestMapping("/getProPlotProId")
    @ResponseBody
    public Map<String, Object> getProPlotProId(HttpServletRequest request) {
        boolean flag = true;
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String proCode = request.getParameter("proCode");
            List<PreProject> preProject = preProjectService.selectPreProject(proCode);
            if (preProject.size() == 1) {
                map.put("proList", preProject);
            }
        }
        catch (Exception e) {
            log.error("根据项目ID获取项目信息异常", e);
            flag = false;
        }
        map.put("flag", flag);
        return map;
    }

}
